package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/09/02 17:47
 **/
public class Day48_3_Offer37_Serialize {
    public class Codec {
        private int post = -1;
        // Encodes a tree to a single string.
        public String serialize(TreeNode root) {
            String s = "";
            if(root == null) return "&,";
            s += root.val + ",";
            s += serialize(root.left);
            s += serialize(root.right);
            return s;
        }

        // Decodes your encoded data to tree.
        public TreeNode deserialize(String data) {
            String[] ch = data.split(",");
            return deserializeUp(ch);
        }

        private TreeNode deserializeUp(String[] ch){
            post++;
            String cur = ch[post];
            TreeNode node;
            if(!cur.equals("&")){
                node = new TreeNode(Integer.parseInt(cur));
                node.left = deserializeUp(ch);
                node.right = deserializeUp(ch);
            }else return null;
            return node;
        }
    }
}